<!--
 * @Author: wwssaabb
 * @Date: 2021-07-20 16:28:06
 * @LastEditTime: 2021-07-20 17:47:48
 * @FilePath: \demo\react_demo\demo\base_demo\生命周期.html
-->

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="test"></div>

  <script type="text/javascript" src="../js/react.development.js" ></script>
  <script type="text/javascript" src="../js/react-dom.development.js" ></script>
  <script type="text/javascript" src="../js/babel.min.js" ></script>
  <script type="text/javascript" src="../js/prop-types.js" ></script>
  <script type="text/babel">  /* JSX 语法用babel转换 */

    class Demo extends React.Component{
      state={
        opacity:1,
      }

      constructor(props){
        console.log('constructor')
        super(props)
      }

      unmounted=()=>{
        ReactDOM.unmountComponentAtNode(document.getElementById('test'))
      }
      
      componentWillMount(){
        console.log('componentWillMount')
      }

      render(){
        console.log('render')
        return (
          <div>
            <h2 style={{opacity:this.state.opacity}}>React 简单吗？</h2>
            <button onClick={this.unmounted}>简单</button>
          </div>
        )
      }

      componentDidMount(){
        console.log('componentDidMount')
        this.timer=setInterval(()=>{
          let {opacity}=this.state
          opacity-=.1
          if(opacity<=0)opacity=1
          this.setState({opacity})
        },200)
      }

      shouldComponentUpdate(){
        console.log('shouldComponentUpdate')
        return false
      }

      componentWillUpdate(){
        console.log('componentWillUpdate')

      }

      componentWillUnmount(){
        console.log('componentWillUnmount')
        clearInterval(this.timer)
      }

      
    }
    
    ReactDOM.render(<Demo/>,document.getElementById('test'))

  </script>
</body>
</html>